package com.kaizeli.website.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kaizeli.website.mapper.ContractPersonnelMapper;
import com.kaizeli.website.pojo.entity.ContractPersonnelDO;
import com.kaizeli.website.response.Result;
import com.kaizeli.website.service.ContractPersonnelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 合同人员服务实现类
 */
@Slf4j
@Service
public class ContractPersonnelServiceImpl extends ServiceImpl<ContractPersonnelMapper, ContractPersonnelDO> implements ContractPersonnelService {
    
    @Override
    public Result<List<ContractPersonnelDO>> getPersonnelByProjectCode(String projectCode) {
        log.info("根据项目编号获取合同人员列表，项目编号：{}", projectCode);
        
        try {
            QueryWrapper<ContractPersonnelDO> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("project_code", projectCode);
            queryWrapper.orderByDesc("create_time");
            
            List<ContractPersonnelDO> personnelList = list(queryWrapper);
            return Result.success(personnelList);
        } catch (Exception e) {
            log.error("根据项目编号获取合同人员列表失败", e);
            return Result.error(500, "获取合同人员列表失败：" + e.getMessage());
        }
    }
    
    @Override
    public Result<ContractPersonnelDO> createContractPersonnel(ContractPersonnelDO contractPersonnel) {
        log.info("创建合同人员，人员姓名：{}", contractPersonnel.getPersonnelName());
        
        try {
            contractPersonnel.setCreateTime(LocalDateTime.now());
            contractPersonnel.setUpdateTime(LocalDateTime.now());
            
            boolean success = save(contractPersonnel);
            if (success) {
                return Result.success(contractPersonnel);
            } else {
                return Result.error(500, "创建合同人员失败");
            }
        } catch (Exception e) {
            log.error("创建合同人员失败", e);
            return Result.error(500, "创建合同人员失败：" + e.getMessage());
        }
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<List<ContractPersonnelDO>> batchCreateContractPersonnel(List<ContractPersonnelDO> contractPersonnelList) {
        log.info("批量创建合同人员，数量：{}", contractPersonnelList.size());
        
        try {
            LocalDateTime now = LocalDateTime.now();
            contractPersonnelList.forEach(personnel -> {
                personnel.setCreateTime(now);
                personnel.setUpdateTime(now);
            });
            
            boolean success = saveBatch(contractPersonnelList);
            if (success) {
                return Result.success(contractPersonnelList);
            } else {
                return Result.error(500, "批量创建合同人员失败");
            }
        } catch (Exception e) {
            log.error("批量创建合同人员失败", e);
            return Result.error(500, "批量创建合同人员失败：" + e.getMessage());
        }
    }
    
    @Override
    public Result<ContractPersonnelDO> updateContractPersonnel(ContractPersonnelDO contractPersonnel) {
        log.info("更新合同人员，ID：{}", contractPersonnel.getId());
        
        try {
            contractPersonnel.setUpdateTime(LocalDateTime.now());
            
            boolean success = updateById(contractPersonnel);
            if (success) {
                return Result.success(contractPersonnel);
            } else {
                return Result.error(500, "更新合同人员失败");
            }
        } catch (Exception e) {
            log.error("更新合同人员失败", e);
            return Result.error(500, "更新合同人员失败：" + e.getMessage());
        }
    }
    
    @Override
    public Result<Void> deleteContractPersonnel(Long id) {
        log.info("删除合同人员，ID：{}", id);
        
        try {
            boolean success = removeById(id);
            if (success) {
                return Result.success();
            } else {
                return Result.error(500, "删除合同人员失败");
            }
        } catch (Exception e) {
            log.error("删除合同人员失败", e);
            return Result.error(500, "删除合同人员失败：" + e.getMessage());
        }
    }
}